import 'package:xws/helpers/as_t.dart';
import 'package:xws/res/images.dart';

/// 用户对象
class User {
  User({
    required this.id,
    required this.nickName,
    required this.description,
    required this.avatarUrl,
    required this.gender,
    required this.country,
    required this.province,
    required this.city,
    required this.region,
    required this.latitude,
    required this.longitude,
    required this.integral,
    required this.level,
    required this.createdAt,
    required this.friendFlag,
  });

  factory User.fromJson(Map<String, dynamic> jsonRes) {
    String _city = asT<String>(jsonRes['city']);
    if (_city.isEmpty) _city = '未知';

    String _avatarUrl = asT<String>(jsonRes['avatarUrl']);
    if (_avatarUrl.isEmpty) _avatarUrl = R.defHeader;

    String _description = asT<String>(jsonRes['description']);
    if (_description.isEmpty) _description = '这个人什么都没有写';

    return User(
      id: asT<String>(jsonRes['id']),
      nickName: asT<String>(jsonRes['nickName']),
      description: _description,
      avatarUrl: _avatarUrl,
      gender: asT<String>(jsonRes['gender']),
      country: asT<String>(jsonRes['country']),
      province: asT<String>(jsonRes['province']),
      city: _city,
      region: asT<String>(jsonRes['region']),
      latitude: asT<double>(jsonRes['latitude']),
      longitude: asT<double>(jsonRes['longitude']),
      integral: asT<int>(jsonRes['integral']),
      level: asT<int>(jsonRes['level']),
      createdAt: asT<String>(jsonRes['createdAt']),
      friendFlag: asT<bool>(jsonRes['friendFlag']),
    );
  }

  late String id;
  late String nickName;
  late String description;
  late String avatarUrl;
  late String gender;
  late String country;
  late String province;
  late String city;
  late String region;
  late double latitude;
  late double longitude;
  late int integral;
  late int level;
  late String createdAt;
  late bool friendFlag;
}
